<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import _ from 'lodash'
import resize from '../mixins/resize'

const animationDuration = 6000

export default {
  name: "BarChart",
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    }
  },
  data() {
    return {
      chart: null,
      options:{
          tooltip: {
              trigger: 'axis',
              axisPointer: { // 坐标轴指示器，坐标轴触发有效
                  type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
              }
          },
          legend:{
              data: ['A','B']
          },
          grid: {
              top: 10,
              left: '2%',
              right: '2%',
              bottom: '3%',
              containLabel: true
          },
          xAxis: {
              type: 'category',
              data: [],
              axisTick: {
                  alignWithLabel: true
              }
          },
          yAxis: {
              type: 'value',
              axisTick: {
                  show: false
              }
          },
          series: [{
              name: 'A',
              type: 'bar',
              data: [],
              animationDuration
          },
            {
              name: 'B',
              type: 'bar',
              data: [],
              animationDuration
          }]
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  methods: {
    initChart() {
      this.chart = this.$echarts.init(this.$el, 'macarons')
      this.setOptions()
    },
    async setOptions() {
        const data ={
            "xAxis": {
                "type": "category",
                "data": [
                    "唧唧",
                    "歪歪",
                    "嘻嘻",
                    "哈哈",
                    "五五",
                    "六六",
                    "七七"
                ]
            },
            "series":[
                {
                    "name": "A",
                    "data": [100, 120, 161, 134, 105, 160, 165]
                },
                {
                    "name": "B",
                    "data": [150, 160, 111, 154, 195, 60, 95]
                }
            ]
        }
        const result = _.merge(this.options,data)
        this.chart.setOption(result)
    }
  },
  beforeDestroy() {
      if (!this.chart) {
          return
      }
      this.chart.dispose()
      this.chart = null
  },
}
</script>
